<?php
/**
 * 
 * Helper for "app" links within Abovo
 * 
 * Creates a link from app class to module/app. For example:
 * `Abovo_App_Crm_Companies`  becomes a href to crm/companies.
 * 
 * @category Abovo
 * 
 * @package Abovo_View_Helper
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 */

/**
 * 
 * Helper for "app" links within Abovo
 * 
 * @category Abovo
 * 
 * @package Abovo_View_Helper
 * 
 */
class Abovo_View_Helper_Avatar extends Solar_View_Helper {
    
    /**
     * 
     * Config keys
     * 
     * @var array
     * 
     */
    public $_Abovo_View_Helper_Avatar = array(
        'path' => 'avatar/small',
    );
    
    /**
     * 
     * Constructor
     * 
     * @return void
     * 
     */
    public function __construct($config = null)
    {
        parent::__construct($config);
        
        // fix uri
        $this->_config['path'] = rtrim($this->_config['path'], '/') . '/';
        
        $this->_obj_avatar = Solar::factory('Abovo_Image_Avatar');
    }
    
    /**
     * 
     * Returns an <img ... /> tag.
     * 
     * If an "alt" attribute is not specified, will add it from the
     * image [[php::basename() | ]].
     * 
     * @param string $src The href to the image source.
     * 
     * @param array $attribs Additional attributes for the tag.
     * 
     * @return string An <img ... /> tag.
     * 
     */
    public function avatar($handle, $attribs = array())
    {
        unset($attribs['src']);
        
        $uri = Solar::factory('Solar_Uri');
        
        if (! $this->_obj_avatar->smallExists($handle)) {
            $src = $uri->quick($this->_config['path'] . 'noimage' . '.jpg');
        } else {
            $src = $uri->quick(
                $this->_config['path']
                . $this->_obj_avatar->getName($handle) 
                . '.jpg'
            );
        }
        
        return '<img src="' . $src . '"'
             . $this->_view->attribs($attribs) . ' />';
    }
}
